package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.SASMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.SAS;
import com.bishe.psy.service.ISasService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-25
 * Time: 19:00
 */

@Slf4j
@Service
public class SasServiceImpl implements ISasService {

    @Resource
    private SASMapper sasMapper;

    @Override
    public void create(SAS sas) {

        //1.参数校验
        if(sas == null || sas.getUserId() == null || sas.getUserId() <= 0
         || sas.getAnxiety_Score() == null || sas.getAnxiety_Score() <=0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.设置默认值
        sas.setDeleteState((byte) 0);
        Date date = new Date();
        sas.setCreateState(date);
        sas.setUpdateState(date);

        //3.调用数据库
        int row = sasMapper.insertSelective(sas);

        //4.判断是否插入成功
        if(row <= 0 ) {
            log.warn("scl90测试问卷插入" + ResultCode.FAILED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public SAS selectByIdNewResult(Long userId) {
        //1.参数校验
        if(userId == null || userId <= 0) {
            log.warn("传入id为空");
            throw new ApplicationException(AppResult.failed("传入参数为空"));
        }

        //2.根据用户id查询最新记录
        SAS sas = sasMapper.selectByIdNewResult(userId);

        //3.返回结果
        return sas;
    }

    @Override
    public List<SAS> selectByUserId(Long userId) {
        //1.参数校验
        if(userId == null || userId <= 0) {
            log.warn("传入id为空");
            throw new ApplicationException(AppResult.failed("传入参数为空"));
        }

        //2.调用数据库
        List<SAS> sas = sasMapper.selectByUserId(userId);

        //返回结果
        return sas;
    }
}
